package cn.com.tuns.assess.qsb.frame.ui;

import android.Manifest;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

public class BaseActivity extends AppCompatActivity {

    private RequestPermissionListener requestPermissionListener;

    private static ProgressDialog progressDialog;

    // 权限
    public final String[] permissions = {
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO,
    };

    public static List<Activity> activitys = new ArrayList<Activity>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        overridePendingTransition(R.anim.act_open_enter, R.anim.act_open_exit);
        super.onCreate(savedInstanceState);
        activitys.add(this);
    }

    //退出APP
    public static void exit() {

        if (activitys != null && !activitys.isEmpty()) {
            for (Activity act : activitys) {
                if (!act.isFinishing()) {
                    act.finish();
                }
            }
            activitys.clear();
        }
    }

    //清除除了MianActivity的其他Activity
//    public static void clearActivityWithOutMain() {
//
//        if (activitys != null && !activitys.isEmpty()) {
//            for (Activity act : activitys) {
//                if (!(act instanceof MainActivity) && !act.isFinishing()) {
//                    act.finish();
//                }
//            }
//            activitys.clear();
//        }
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        activitys.remove(this);

    }

    public void setRequestPermissionListener(RequestPermissionListener requestPermissionListener) {
        this.requestPermissionListener = requestPermissionListener;
    }


    public interface RequestPermissionListener {

        void onRequestPermissionListener(int requestCode, boolean granted);
    }

    /**
     * 检查是否已被授权危险权限
     */
    public boolean checkDangerousPermissions(String[] permissions) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true;
        }
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED ||
                    ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 请求权限
     */
    public void requestDangerousPermissions(String[] permissions, int requestCode) {
        if (checkDangerousPermissions(permissions)) {
            handlePermissionResult(requestCode, true);
            return;
        }
        ActivityCompat.requestPermissions(this, permissions, requestCode);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        boolean granted = true;
        for (int result : grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                granted = false;
                break;
            }
        }
        handlePermissionResult(requestCode, granted);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }

    /**
     * 处理请求危险权限的结果
     */
    public void handlePermissionResult(int requestCode, boolean granted) {
        if (requestPermissionListener != null) {
            requestPermissionListener.onRequestPermissionListener(requestCode, granted);
        }
    }

    public static void showProgressDialog(Activity activity,String msg, boolean iscancle) {
        if (progressDialog==null||!progressDialog.isShowing()) {
            progressDialog = new ProgressDialog(activity);
            progressDialog.setMessage(msg);
            progressDialog.setCancelable(iscancle);
            progressDialog.setCanceledOnTouchOutside(iscancle);
            progressDialog.show();
        }
    }

    public static void dimissProDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog=null;
        }
    }
}
